home *** CD-ROM | disk | FTP | other *** search
/ FM Towns: Free Software Collection 8 / FM Towns Free Software Collection 8.iso / t_os / wink24h / src / prnout.c < prev    next >
Text File  |  1994-06-01  |  12KB  |  440 lines

  1. #include    <stdio.h>
  2. #include    <ctype.h>
  3. #include    "defs.h"
  4.  
  5. #define UNSIG   unsigned short int
  6. #define PRNDEV  "PRN"
  7.  
  8. #define PRNIO   0
  9. #define FUJITU  1
  10. #define ESCP    2
  11.  
  12. extern void  prn_out();
  13. extern int   PRB_chr();
  14. extern void  wrtstr();
  15. extern void  Dmy_form();
  16. extern int   kbhit();
  17. extern int   Get_key();
  18.  
  19. static int     left_mrg=12;
  20. static int     head_pos=0;
  21. static int     head_max=80;
  22. static int     line_pos=0;
  23. static int     line_max=58;
  24. static int     page_pos=0;
  25. static short   prn_flg=FALSE;
  26. static short   prn_mode=FUJITU;
  27. static short   prn_page=FALSE;
  28. static short   prn_kin=FALSE;
  29. static short   prn_ryo=TRUE;
  30. static short   prn_odd=FALSE;
  31. static FILE    *out_fp=stdout;
  32.  
  33. static UNSIG ank_tbl[]={
  34.         0x8140,0x8149,0x8168,0x8194,0x8190,0x8193,0x8195,0x8166,
  35.         0x8169,0x816A,0x8196,0x817B,0x8143,0x817C,0x8144,0x815E,
  36.         0x824F,0x8250,0x8251,0x8252,0x8253,0x8254,0x8255,0x8256,
  37.         0x8257,0x8258,0x8146,0x8147,0x8183,0x8181,0x8184,0x8148,
  38.         0x8197,0x8260,0x8261,0x8262,0x8263,0x8264,0x8265,0x8266,
  39.         0x8267,0x8268,0x8269,0x826A,0x826B,0x826C,0x826D,0x826E,
  40.         0x826F,0x8270,0x8271,0x8272,0x8273,0x8274,0x8275,0x8276,
  41.         0x8277,0x8278,0x8279,0x816D,0x818F,0x816E,0x814F,0x8151,
  42.         0x8166,0x8281,0x8282,0x8283,0x8284,0x8285,0x8286,0x8287,
  43.         0x8288,0x8289,0x828A,0x828B,0x828C,0x828D,0x828E,0x828F,
  44.         0x8290,0x8291,0x8292,0x8293,0x8294,0x8295,0x8296,0x8297,
  45.         0x8298,0x8299,0x829A,0x816F,0x8162,0x8170,0x8150,0x85A1 };
  46. static UNSIG kana_tbl[]={
  47.         0x8140,0x8142,0x8175,0x8176,0x8141,0x8145,0x8392,0x8340,
  48.         0x8342,0x8344,0x8346,0x8348,0x8383,0x8385,0x8387,0x8362,
  49.         0x815B,0x8341,0x8343,0x8345,0x8347,0x8349,0x834A,0x834C,
  50.         0x834E,0x8350,0x8352,0x8354,0x8356,0x8358,0x835A,0x835C,
  51.         0x835E,0x8360,0x8363,0x8365,0x8367,0x8369,0x836A,0x836B,
  52.         0x836C,0x836D,0x836E,0x8371,0x8374,0x8377,0x837A,0x837D,
  53.         0x837E,0x8380,0x8381,0x8382,0x8384,0x8386,0x8388,0x8389,
  54.         0x838A,0x838B,0x838C,0x838D,0x838F,0x8393,0x814A,0x814B };
  55.  
  56. #ifdef    TOWNS
  57. UNSIG   sjistojis(cd)
  58. UNSIG   cd;
  59. {
  60.     int    hi,lo;
  61.  
  62.     hi=(cd >> 8)&0xff;
  63.     lo=cd & 0xff;
  64.     hi -= ( hi <= 0x9f) ? 0x71 : 0xb1;
  65.     hi = hi * 2 +1;
  66.     if ( lo > 0x7f )
  67.         lo--;
  68.     if ( lo >= 0x9e ) {
  69.         lo -= 0x7d;
  70.         hi++;
  71.     }
  72.     else
  73.         lo -= 0x1f;
  74.     return (hi << 8 | lo);
  75. }
  76. #endif
  77. static UNSIG   han_to_zen(code)
  78. UNSIG   code;
  79. {
  80.     if ( 0x20 <= code && code <= 0x7F )
  81.         return ank_tbl[code-0x20];
  82.     else if ( 0xA0 <= code && code <= 0xDF )
  83.         return kana_tbl[code-0xA0];
  84.     else
  85.         return 0x85A1;
  86. }
  87. /*  .(.全角禁足文字のチェック.).  */
  88. static int     kinsoku_chk(i)
  89. UNSIG   i;
  90. {
  91.     if ( prn_kin != FALSE )
  92.     return FALSE;
  93.  
  94.     if ( (i & 0xFF00) == 0 ) {
  95.         switch(i) {
  96.             case 0x27: case 0x29: case 0x2c: case 0x2e: case 0x3a:
  97.             case 0x3b: case 0x7d:
  98.             case 0xa1: case 0xa3: case 0xa4: case 0xde: case 0xdf:
  99.                 return TRUE;
  100.         }
  101.         return FALSE;
  102.     }
  103.  
  104.     if (i >= 0x8141 && i <= 0x814c)
  105.         return TRUE;
  106.     if (i >= 0x8165 && i <= 0x817a) {
  107.         if ((i % 2) == 0)
  108.             return TRUE;
  109.     }
  110.     switch(i) {
  111.         case 0x829f: case 0x82a1: case 0x82a3: case 0x82a5: case 0x82a7:
  112.         case 0x82c1: case 0x82e1: case 0x82e3: case 0x82e5: case 0x82ec:
  113.         case 0x8340: case 0x8342: case 0x8344: case 0x8346: case 0x8348:
  114.         case 0x8362: case 0x8383: case 0x8385: case 0x8387: case 0x838e:
  115.         case 0x8395: case 0x8396:
  116.             return TRUE;
  117.     }
  118.     return FALSE;
  119. }
  120. static void    prn_chr_out(ch)
  121. UCHAR   ch;
  122. {
  123.     int     i,f=FALSE;
  124.  
  125.     if ( prn_ryo == FALSE && prn_odd != (page_pos % 2) )
  126.     return;
  127.     
  128.     if ( prn_mode == PRNIO ) {
  129.     putc(ch,out_fp);
  130.     return;
  131.     }
  132.     while ( (i = PRB_chr(ch)) != 0 ) {
  133.     if ( i == 0x04 )
  134.         wrtstr("プリンタに用紙をセットしてください",16,15,0x02);
  135.     else if ( i == 0x05 )   
  136.         wrtstr("プリンタが用意されていません      ",16,15,0x02);
  137.     else
  138.         wrtstr("プリンタに動作エラ-があります    ",16,15,0x02);
  139.     f = TRUE;
  140.     if ( kbhit() != 0 )
  141.         return;
  142.     }
  143.     if ( f != FALSE )
  144.     wrtstr("                                  ",16,15,0x02);
  145. }
  146. static void    prn_kan_out(code)
  147. UNSIG   code;
  148. {
  149.     if ( prn_mode != PRNIO )
  150.     code = sjistojis(code);
  151.     prn_chr_out((UCHAR)(code >> 8));
  152.     prn_chr_out((UCHAR)code);
  153. }
  154. static void    prn_line_out(str)
  155. char    *str;
  156. {
  157.     while ( *str != '\0' )
  158.         prn_chr_out(*(str++));
  159. }
  160. static void    page_out()
  161. {
  162.     int     i;
  163.     char    *p,tmp[20];
  164.  
  165.     head_pos = line_pos = 0;
  166.     prn_out('\n');
  167.     for ( i = head_max / 2 - 2 ; i > 0 ; i-- )
  168.         prn_out(' ');
  169.     sprintf(tmp,"-%d-\n",page_pos+1);
  170.     for ( p = tmp ; *p != '\0' ; p++ )
  171.         prn_out(*p);
  172. }
  173. void    prn_out(code)
  174. UNSIG   code;
  175. {
  176.     int     i;
  177.  
  178.     if ( code >= ' ' &&
  179.         (head_pos + ((code & 0xFF00) != 0 ? 2:1)) > head_max &&
  180.          kinsoku_chk(code) == FALSE )
  181.         prn_out('\n');
  182.  
  183.     if ( head_pos == 0 && code >= 0x20 ) {
  184.         switch(prn_mode) {
  185.             case FUJITU: prn_line_out("\x1bQ1 |"); break;
  186.             case ESCP:   prn_line_out("\x1cU\x1c&\x1c\x0f");
  187.                          prn_flg = FALSE; break;
  188.         }
  189.         for ( i = 0 ; i < left_mrg ; i++ ) {
  190.             head_pos = ERR;
  191.             prn_out(' ');
  192.         }
  193.         head_pos = 0;
  194.     }
  195.  
  196.     if ( (code & 0xFF00) != 0 ) {
  197.         switch(prn_mode) {
  198.             case FUJITU:
  199.                 if ( prn_flg == FALSE )
  200.                     prn_line_out("\x1b$B");
  201.                 break;
  202.             case ESCP:
  203.                 if ( prn_flg == FALSE )
  204.                     prn_line_out("\x1c\x12");
  205.                 break;
  206.         }
  207.         prn_flg = TRUE;
  208.         prn_kan_out(code);
  209.         head_pos += 2;
  210.     } else if ( code >= 0x20 ) {
  211.         switch(prn_mode) {
  212.             case FUJITU:
  213.                 if ( prn_flg != FALSE )
  214.                     prn_line_out("\x1b(H");
  215.                 prn_chr_out((UCHAR)code);
  216.                 break;
  217.             case ESCP:
  218.                 if ( prn_flg != FALSE )
  219.                     prn_line_out("\x1c\x0f");
  220.                 code = han_to_zen(code);
  221.                 prn_kan_out(code);
  222.                 break;
  223.             case PRNIO:
  224.                 prn_chr_out((UCHAR)code);
  225.                 break;
  226.         }
  227.         prn_flg = FALSE;
  228.         head_pos++;
  229.     } else if ( code == '\t' ) {
  230.         i = 8 - (head_pos & 0x07);
  231.         while ( i-- > 0 )
  232.             prn_out(' ');
  233.     } else if ( code == '\x0C' ) {
  234.         if ( prn_page == FALSE ) {
  235.             while ( line_pos++ < line_max )
  236.                 prn_chr_out('\n');
  237.             page_out();
  238.         }
  239.         head_pos = line_pos = 0;
  240.         prn_chr_out('\x0C');
  241.     page_pos++;
  242.     } else if ( code == '\n' ) {
  243.         prn_chr_out((UCHAR)code);
  244.         prn_chr_out((UCHAR)'\r');
  245.         head_pos = 0;
  246.         if ( ++line_pos >= line_max )
  247.             prn_out('\x0C');
  248.     } else
  249.         prn_chr_out((UCHAR)code);
  250. }
  251. int    prn_init(lmrg,hmax,lmax,page,kin,ryo,mode)
  252. int    lmrg,hmax,lmax,page,kin,ryo,mode;
  253. {
  254.     left_mrg = lmrg;
  255.     head_pos = 0;
  256.     head_max = hmax;
  257.     line_pos = 0;
  258.     line_max = lmax;
  259.     page_pos = 0;
  260.     prn_flg  = FALSE;
  261.     prn_mode = mode;
  262.     prn_page = page;
  263.     prn_kin  = kin;
  264.     prn_ryo  = ryo;
  265.  
  266.     if ( prn_page == FALSE )
  267.     line_max -= 2;
  268.  
  269.     if ( prn_mode == PRNIO && (out_fp = fopen(PRNDEV,"w")) == NULL )
  270.     return ERR;
  271.  
  272.     switch(prn_mode) {
  273.         case FUJITU:
  274.         prn_line_out("\x1bQ3 \x5c\x1c$\x22\x77\x1b[30;18 G\x1b(H");
  275.         break;
  276.         case ESCP:
  277.         prn_line_out("\x1c\x0f");
  278.         break;
  279.     }
  280.     return FALSE;
  281. }
  282. void    prn_end()
  283. {
  284.     prn_out('\x0C');
  285.     if ( prn_mode == PRNIO )
  286.     fclose(out_fp);
  287. }
  288. void    Printer_out()
  289. {
  290.     int    no,i,n,yo,cl,ch,ec,mrg;
  291.     int    x=20,y=6,old_page=(-1);
  292.     LONG   l;
  293.     char   tmp[160];
  294.     static char   lmrg_tmp[8],rmrg_tmp[8];
  295.     static char   hmax_tmp[8],lmax_tmp[8];
  296.     static struct {
  297.     short    lmrg_pp;
  298.     short    rmrg_pp;
  299.     short    hmax_pp;
  300.     short    lmax_pp;
  301.     } yousi[]={
  302.     { 3,3,72,39 },{ 6,6,80,49 },{ 12,12,80,59 },{ 12,12,110,75 }
  303.     };
  304.     static struct {
  305.     short    val_mn;
  306.     short   max_mn;
  307.     char    *ttl_mn;
  308.     char    *sub_mn[4];
  309.     } menu[]={
  310.     { 2,4,"用紙の選択","A5","B5","A4","B4" },
  311.     { 0,1,"左の 余 白",lmrg_tmp },
  312.     { 0,1,"右の 余 白",rmrg_tmp },
  313.     { 0,1,"印字文字数",hmax_tmp },
  314.     { 0,1,"印字 行 数",lmax_tmp },
  315.     { 0,2,"ペ-ジ印字","する","しない" },
  316.     { 0,2,"禁則 処 理","する","しない" },
  317.     { 1,2,"両面 印 刷","する","しない" },
  318.     { 0,3,"使用プリンタ","PRN","FM系","ESC/P" }
  319.     };
  320.  
  321.     Dmy_form(tmp,42,0x98,0x95,0x99); 
  322.     wrtstr(tmp,x,y,0x07);
  323.     Dmy_form(tmp,42,0x96,0x20,0x96); 
  324.     for ( i = 0 ; i < 9 ; i++ )
  325.     wrtstr(tmp,x,y+i+1,0x07);
  326.     Dmy_form(tmp,42,0x9A,0x95,0x9B); 
  327.     wrtstr(tmp,x,y+10,0x07);
  328.     x += 2; y += 1;
  329.  
  330.     for ( no = 0 ; ; ) {
  331.     yo = menu[0].val_mn;
  332.     sprintf(lmrg_tmp,"%d    ",yousi[yo].lmrg_pp);
  333.     sprintf(rmrg_tmp,"%d    ",yousi[yo].rmrg_pp);
  334.     sprintf(hmax_tmp,"%d    ",yousi[yo].hmax_pp);
  335.     sprintf(lmax_tmp,"%d    ",yousi[yo].lmax_pp);
  336.     for ( i = 0 ; i < 9 ; i++ ) {
  337.         if ( i == no ) cl = 0x14; else cl = 0x07;
  338.         wrtstr(menu[i].ttl_mn,x,y+i,cl);
  339.         for ( n = 0 ; n < menu[i].max_mn ; n++ ) {
  340.         if ( menu[i].max_mn > 1 && n == menu[i].val_mn )
  341.             cl = 0x14;
  342.         else
  343.             cl = 0x07;
  344.         wrtstr(menu[i].sub_mn[n],x+14+n*7,y+i,cl);
  345.         }
  346.     }
  347.     ch = Get_key(&ec);
  348.     if ( ch == '\x1B' || ec == 0x7200 )
  349.         return;
  350.         else if ( ch == '\x0D' || ec == 0x7300 )
  351.         break;    
  352.     else if ( ch == ' ' || ch == '\x1F' ) {
  353.         if ( ++no >= 9 )
  354.         no = 0;
  355.     } else if ( ch == '\x08' || ch == '\x1E' ) {
  356.         if ( --no < 0 )
  357.         no = 8;
  358.     } else if ( ch == '\x1C' ) {
  359.         if ( no == 1 )
  360.         yousi[yo].lmrg_pp += 1;
  361.         else if ( no == 2 )
  362.         yousi[yo].rmrg_pp += 1;
  363.         else if ( no == 3 )
  364.         yousi[yo].hmax_pp += 1;
  365.         else if ( no == 4 )
  366.         yousi[yo].lmax_pp += 1;
  367.         else {
  368.             menu[no].val_mn += 1;
  369.             if ( menu[no].val_mn >= menu[no].max_mn )
  370.             menu[no].val_mn = 0;
  371.         }
  372.     } else if ( ch == '\x1D' ) {
  373.         if ( no == 1 )
  374.         yousi[yo].lmrg_pp -= 1;
  375.         else if ( no == 2 )
  376.         yousi[yo].rmrg_pp -= 1;
  377.         else if ( no == 3 )
  378.         yousi[yo].hmax_pp -= 1;
  379.         else if ( no == 4 )
  380.         yousi[yo].lmax_pp -= 1;
  381.         else {
  382.             menu[no].val_mn -= 1;
  383.         if ( menu[no].val_mn < 0 )
  384.             menu[no].val_mn = menu[no].max_mn - 1;
  385.         }
  386.     }
  387.     }
  388.     x = 13; y = 12;
  389.     Dmy_form(tmp,54,0x98,0x95,0x99);
  390.     wrtstr(tmp,x,y,0x05);
  391.     Dmy_form(tmp,54,0x96,0x20,0x96);
  392.     for ( i = 1 ; i <= 5 ; i++ )
  393.     wrtstr(tmp,x,y+i,0x05);
  394.     Dmy_form(tmp,54,0x9A,0x95,0x9B);
  395.     wrtstr(tmp,x,y+6,0x05);
  396.     wrtstr("印刷を中断する場合は,[取消]キ-を押してください",x+3,y+4,0x02);
  397.  
  398.     prn_odd = 0; mrg = yousi[yo].lmrg_pp;
  399. REPRINT:
  400.     prn_init(mrg,
  401.          yousi[yo].hmax_pp,
  402.          yousi[yo].lmax_pp,
  403.          menu[5].val_mn,
  404.          menu[6].val_mn,
  405.          menu[7].val_mn,
  406.          menu[8].val_mn);
  407.  
  408.     for ( l = 0l ; l < btm_ptr ; ) {
  409.     if ( kbhit() != 0 ) {
  410.         ch = Get_key(&ec);
  411.         if ( ec == 0x7200 ) {
  412.         prn_end();
  413.         return;
  414.         }
  415.     }
  416.     ch = *Cnv_ptr(l++);
  417.     if ( iskanji(ch) )
  418.         ch = (ch << 8) | *Cnv_ptr(l++);
  419.     if ( ch != '\r' )
  420.         prn_out(ch);
  421.     if ( page_pos != old_page ) {
  422.         if ( prn_ryo != FALSE || prn_odd == (page_pos % 2) ) {
  423.    sprintf(tmp,"只今%dペ-ジ目を印刷しています             ",page_pos+1);
  424.             wrtstr(tmp,16,14,0x02);
  425.         }
  426.         old_page = page_pos;
  427.     }
  428.     }
  429.     prn_end();
  430.  
  431.     if ( prn_ryo == FALSE && prn_odd == 0 ) {
  432.     prn_odd = 1; mrg = yousi[yo].rmrg_pp;
  433.         wrtstr("用紙を裏返しにして何かキ-を押してください",16,14,0x02);
  434.     while ( kbhit() == 0 );
  435.     ch = Get_key(&ec);
  436.     if ( ec != 0x7200 )
  437.         goto REPRINT;
  438.     }
  439. }
  440.